<?php

if (!isset($_REQUEST['user']) || !is_numeric($_REQUEST['user'])) {
    error_obj::setError(new error_obj("bad request user", __FILE__, 'line 4 -> redirect'), UNEXCEPTED);
    base_obj::redirect(ROOT);
}

$userId = (int) $_REQUEST['user'];

$user_obj = new user_obj();
$client_obj = new client_obj();

if (!$user_obj->visibleUser($userId)){
    error_obj::setError(new error_obj("not visible task", __FILE__, 'line 14 -> redirect'), UNEXCEPTED);
    base_obj::redirect(ROOT . '?input=list_of_users');
}

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'edit_user':
            $user = $_REQUEST['user_data'];

            if (empty($user['password']))
                unset($user['password']);
            else
                $user['password'] = md5($user['password']);

            $associatedClients = array();
            if ($user['userlevel'] == USER_TYPE_CLIENT && !empty($user['associatedClients'])) {
                foreach ($user['associatedClients'] as $key => $clientId) {
                    $associatedClients[$clientId] = $clientId;
                }
            }
            $associatedClients[$user['client']] = $user['client'];
            unset($user['associatedClients']);

            $result = $user_obj->editUser($user, $userId);
            if (error_obj::isError($result)) {
                error_obj::setError($result, UNEXCEPTED);
            } else {
                $user_obj->deleteAssociatedClientsByUserId($userId);
                foreach ($associatedClients as $k => $clientId) {
                    $in_data = array();
                    $in_data['user_id'] = $userId;
                    $in_data['client_id'] = $clientId;
                    $result = $user_obj->addAssociatedClient($in_data);
                }
                error_obj::setGood('user was updated!');
                base_obj::redirect(ROOT . '?input=list_of_users');
            }

            break;
    }
}

$user_data = $user_obj->getUserById($userId);

if (error_obj::isError($user_data)) {
    error_obj::setError($user_data, UNEXCEPTED);
} elseif (empty($user_data)) {
    error_obj::setError(new error_obj("empty user", __FILE__, 'get user data'), EMPTY_USER);
} else {

    if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_TECHNIC) {
        $isAssigned = $user_obj->isAssignedClientToTechnic($userId);
        if (error_obj::isError($isAssigned) || !$isAssigned || $userId != $isAssigned) {
            error_obj::setError(new error_obj("bad request user", __FILE__, 'line 4 -> redirect'), UNEXCEPTED);
            base_obj::redirect(ROOT . "?input=list_of_users");
        }
    }

    $filter = array();
    $filter['active'] = 0;
    if ($_SESSION['user']['profile']['userlevel'] == USER_TYPE_CLIENT) {
        $filter['clients'] = $_SESSION['user']['clients'];
    }
    $user['clients'] = $client_obj->getAllClients('contactPerson, klient_id, name, bad_payer', $filter, 'name ASC');

    $user['userLevels'] = $GLOBALS['userLevels'];

    $GLOBALS['template_data'] = $user;
    $GLOBALS['template_data']['user_data'] = $user_data;
    $associatedClients = $user_obj->getAssociatedClientsByUserId($userId);
    if (error_obj::isError($associatedClients)) {
        error_obj::setError($associatedClients, UNEXCEPTED);
    } else {
        $GLOBALS['template_data']['user_data']['associatedClients'] = $associatedClients;
    }
}
?>
